/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EstagiarioListaUI.java
 *
 * Created on 22/06/2011, 17:33:34
 */


package my.contacteditor;

/**
 *
 * @author Felipe
 */
public class EstagiarioListaUI extends javax.swing.JFrame {

    /** Creates new form EstagiarioListaUI */
    public EstagiarioListaUI() {
        initComponents();
         Estagiario estagiario = new Estagiario();
       lista.setModel(estagiario.ListarTodos());    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        lista = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtnomeestagiario = new javax.swing.JTextField();
        btnAlterar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        lblCodigo = new javax.swing.JLabel();
        mensagem = new javax.swing.JLabel();
        InserirNovo = new javax.swing.JButton();
        txtequipresponsavel = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtcronograma = new javax.swing.JTextArea();
        jLabel7 = new javax.swing.JLabel();
        lblcodigo = new javax.swing.JLabel();
        combosetresponsavel = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lista.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        lista.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(lista);

        jPanel1.setBorder(new javax.swing.border.MatteBorder(null));

        jLabel3.setText("Codigo");

        jLabel2.setText("Nome do Estagiario:");

        txtnomeestagiario.setName("txtnomeestagiario"); // NOI18N

        btnAlterar.setText("Alterar");
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        mensagem.setForeground(new java.awt.Color(255, 0, 0));

        InserirNovo.setText("Inserir novo");
        InserirNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                InserirNovoActionPerformed(evt);
            }
        });

        jLabel1.setText("Equipamento Responsavel:");

        jLabel6.setText("Setor Responsavel:");

        jLabel4.setText("ITEM SELECIONADO");

        txtcronograma.setColumns(20);
        txtcronograma.setRows(5);
        jScrollPane2.setViewportView(txtcronograma);

        jLabel7.setText("Cronograma:");

        combosetresponsavel.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Almoxarifado", "Bem Estar", "Bromatologia", "Biomolecular", "Comunicação", "Cultura de Tecidos", "Fisiologia", "Financeiro", "Fisica do solos", "Gráfica", "Pesquisa", "Recursos Humanos", "Viagens", " " }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel6)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(combosetresponsavel, 0, 166, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                            .addComponent(txtequipresponsavel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                            .addComponent(lblcodigo, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                            .addComponent(txtnomeestagiario, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblCodigo)
                        .addGap(95, 95, 95)
                        .addComponent(mensagem)
                        .addGap(237, 237, 237))
                    .addComponent(jLabel4)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(btnAlterar)
                        .addGap(49, 49, 49)
                        .addComponent(btnExcluir)
                        .addGap(54, 54, 54)
                        .addComponent(InserirNovo)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblcodigo)
                    .addComponent(jLabel3))
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtnomeestagiario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtequipresponsavel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(combosetresponsavel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addComponent(lblCodigo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(mensagem))
                    .addComponent(jLabel7))
                .addGap(17, 17, 17)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAlterar)
                    .addComponent(btnExcluir)
                    .addComponent(InserirNovo)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 741, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(54, 54, 54)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
 public void
            formWindowActivated(java.awt.event.WindowEvent evt) {
        // TODO add your handling code here:

    }
    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        // TODO add your handling code here:
        Estagiario estagiario = new Estagiario();

        int vnump = Integer.parseInt(lblcodigo.getText());
        boolean retorno = estagiario.Alterar(txtnomeestagiario.getText(),(String)combosetresponsavel.getSelectedItem(),txtequipresponsavel.getText(),txtcronograma.getText(), vnump);
        if(retorno) {

            lista.setModel(estagiario.ListarTodos());
            mensagem.setText("");
            btnAlterar.setEnabled(false);
            btnExcluir.setEnabled(false);
            limparFormulario();
        } else {
            mensagem.setText("Não foi possível alterar");
        }
}//GEN-LAST:event_btnAlterarActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        // TODO add your handling code here:
        // TODO add your handling code here:
        Estagiario estagiario = new Estagiario();

        int vcodigo = Integer.parseInt(lblcodigo.getText());
        boolean retorno = estagiario.Excluir(vcodigo);
        if(retorno) {

            lista.setModel(estagiario.ListarTodos());
            limparFormulario();
        } else {
            mensagem.setText("Não foi possível excluir");
        }
}//GEN-LAST:event_btnExcluirActionPerformed

    private void InserirNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InserirNovoActionPerformed
        // TODO add your handling code here:
        EstagiarioCadastroUI form = new EstagiarioCadastroUI();
        form.setVisible(true);
        this.dispose();
}//GEN-LAST:event_InserirNovoActionPerformed


   
    private void listaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listaMouseClicked
        // TODO add your handling code here:
        int indice = lista.getSelectedRow();
        String nomeestagiario = (String)lista.getValueAt(indice,1);
        String equipresponsavel = (String)lista.getValueAt(indice,2);
      //  String setresponsavel = (String)lista.getValueAt(indice, 3);
        String cronograma = (String)lista.getValueAt(indice,4);
        String codigo = (String)lista.getValueAt(indice,0);

   //     para o alterar tem q buscar o combo selecionado
        txtnomeestagiario.setText(nomeestagiario);
        txtequipresponsavel.setText(equipresponsavel);
    //  txtsetresponsavel.setText(setresponsavel);
        txtcronograma.setText(cronograma);
        lblcodigo.setText(codigo);
        btnAlterar.setEnabled(true);
        btnExcluir.setEnabled(true);
    }//GEN-LAST:event_listaMouseClicked
private void limparFormulario()
    {
        lblCodigo.setText("");
        mensagem.setText("");
        txtnomeestagiario.setText("");

        txtequipresponsavel.setText("");
        
        txtcronograma.setText("");
        //lblcodigo.setText("");
        btnExcluir.setEnabled(false);
        btnAlterar.setEnabled(false);
    }


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EstagiarioListaUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton InserirNovo;
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JComboBox combosetresponsavel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblCodigo;
    private javax.swing.JLabel lblcodigo;
    private javax.swing.JTable lista;
    private javax.swing.JLabel mensagem;
    private javax.swing.JTextArea txtcronograma;
    private javax.swing.JTextField txtequipresponsavel;
    private javax.swing.JTextField txtnomeestagiario;
    // End of variables declaration//GEN-END:variables

}
